﻿@using KRM.Core
@model IEnumerable<KRM.Core.tblMessageToDriver>
@{
    List<LP_Drivers> allDriver = ViewBag.alldriver;
}
<script>
    $(document).ready(function () {
        $("#btntogglereceiver").click(function () {
            $(".receivers").toggle();
            if ($(".receivers").css('display') == "block") {
                $(this).find("i").attr('class', 'icon-ok');
            } else {
                $(this).find("i").attr('class', 'icon-plus');
            }
        });
        $(".receivercheck input").click(function () {
            if ($(this).is(":checked")) {
                $.ajax({
                    url: "/orderview/inputreceivernumber",
                    data: { phone: $(this).val(), listphone: $("#listphone").val() },
                    success: function (data) {
                        $("#listphone").val(data);
                    }
                });
            }
        });

        $(".fr-sentmessagetodriver").submit(function () {
            if ($("#listphone").val() == "") {
                alert('Please enter phone number');
                return false;
            }
            var serializedForm = $(this).serialize();
            App.blockUI($('.fr-sentmessagetodriver'), false);
            $.ajax({
                type: 'POST',
                url: '/orderview/sentmessagetodriver',
                data: serializedForm,
                success: function (data) {
                    var orderid = @ViewBag.OrderId;
                    $.ajax({
                        url:"/orderview/getmessagetodrive?orderid=" + orderid,
                        success: function(res) {
                            $(".sent-items").html(res);
                        }
                    });
                    alert(data);
                    App.unblockUI($('.fr-sentmessagetodriver'));
                },
                error: function () {
                    alert('sent fail!');
                    App.unblockUI($('.fr-sentmessagetodriver'));
                }
            });
            return false;
        });
    });
    function partdetailchange(check) {
        if (check.is(":checked")) {
            $("#messagedriver").html("@ViewBag.Part");
        } else {
            $("#messagedriver").html("");
        }
    }
</script>
<div class="order-email-scroll" style="height: 240px">
    <div class="span4 row-fluid">
        <form action="/orderview/sentmessagetodriver" method="POST" class="fr-sentmessagetodriver margin0">
            <input type="hidden" name="orderid" value="@ViewBag.OrderId"/>
            <p>To:</p>
            <div class="row-fluid">
                <div class="span10">
                    <input type="text" class="input-block-level" name="phones" id="listphone" required="required" />
                </div>
                <div class="span2">
                    <button class="btn btn-success" id="btntogglereceiver" type="button"><i class="icon-plus"></i></button>
                </div>
            </div>
            <div class="row-fluid">
                <div class="span7">
                    Message: (<span class="textcount">0/160</span>)
                </div>
                <div class="span5">
                    <label><input type="checkbox" class="cbcheck" onchange="partdetailchange($(this));"/> Part Detail</label>
                </div>
            </div>
            <div class="row-fluid">
                <div class="span10">
                    <textarea class="input-block-level" name="message" id="messagedriver" style="height: 130px" maxlength="160" required="required"
                              onkeydown="limitText(this);" onkeyup="limitText(this)"></textarea>
                </div>
                <div class="span2">
                    <input type="submit" class="btn btn-primary" value="Send" />
                </div>
            </div>
            <div class="receivers border-padding10">
                @if (allDriver != null)
                {
                    <div class="receivercheck">
                        @foreach (var item in allDriver)
                        {
                            <label>
                                <input type="checkbox" class="cbcheck" value="@item.DriverTelephoneNumber"/>
                                @item.DriverName</label>
                        }
                    </div>
                }
            </div>
        </form>
    </div>
    <div class="span8 sent-items">
        <table class="table table-bordered table-fix">
            <thead>
                <tr>
                    <th>Receiver</th>
                    <th>Message</th>
                    <th>Sent</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.To</td>
                        <td title="@item.Message">@item.Message</td>
                        <td>@item.TimeSent</td>
                        <td>@item.Sent</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
